﻿using IterationLife.System.Models;
using Util;

namespace IterationLife.PlatformBackEnd.AppServices.Dtos.Systems {
    /// <summary>
    /// 行政区域数据传输对象扩展
    /// </summary>
    public static class DistrictDtoExtension {        
        /// <summary>
        /// 转换为行政区域实体
        /// </summary>
        /// <param name="dto">行政区域数据传输对象</param>
        public static District ToEntity( this DistrictDto dto ) {
            if( dto == null )
                return new District();
            return new District( dto.Id.ToGuid() ,dto.Path,dto.Level.SafeValue() ) {
                ParentId = dto.ParentId.ToGuidOrNull(),
                Code = dto.Code,
                ParentCode = dto.ParentCode,
                Name = dto.Name,
                MergerName = dto.MergerName,
                ShortName = dto.ShortName,
                MergerShortName = dto.MergerShortName,
                TelCode = dto.TelCode,
                ZipCode = dto.ZipCode,
                PinYin = dto.PinYin,
                FullPinYin = dto.FullPinYin,
                English = dto.English,
                Longitude = dto.Longitude,
                Latitude = dto.Latitude,
                CodePath = dto.CodePath,
                Enabled = dto.Enabled,
                SortId = dto.SortId,
                Version = dto.Version
            };
        }
        
        /// <summary>
        /// 转换为行政区域数据传输对象
        /// </summary>
        /// <param name="entity">行政区域实体</param>
        public static DistrictDto ToDto( this District entity ) {
            if( entity == null )
                return new DistrictDto();
            return new DistrictDto {
                Id = entity.Id.SafeString(),
                ParentId = entity.ParentId.SafeString(),
                Code = entity.Code,
                ParentCode = entity.ParentCode,
                Name = entity.Name,
                MergerName = entity.MergerName,
                ShortName = entity.ShortName,
                MergerShortName = entity.MergerShortName,
                Level = entity.Level,
                TelCode = entity.TelCode,
                ZipCode = entity.ZipCode,
                PinYin = entity.PinYin,
                FullPinYin = entity.FullPinYin,
                English = entity.English,
                Longitude = entity.Longitude,
                Latitude = entity.Latitude,
                Path = entity.Path,
                CodePath = entity.CodePath,
                Enabled = entity.Enabled,
                SortId = entity.SortId,
                Version = entity.Version
            };
        }
    }
}
